<?php

/*
 * Copyright 2017 zhc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

if(!defined("MY_ROOT"))
    die();



/**
 * Description of STableEditor
 * The table editor service
 * @author Zhu Chen <zhuchen@greatqq.com>
 */
class STableEditor{
    /**
     * A handle of table editor implementation
     * @var TableEditor
     */
    protected $teditor;
    
    /**
     * The class cannot be constructed unless ihnerit it
     * @param TableEditor $tedit
     */
    protected function __construct(TableEditor $tedit) {
        $this->teditor = $tedit;
    }
    
    /**
     * Implement the IPackage
     * @param type $action
     * @return type
     */
    public static function get_includes($action){
        return ['tables/ColumnProperty','tables/tableeditor'];
    }
    
    /**
     * Implement the IPackage
     * @param type $action
     * @return string
     */
    public static function get_function($action)
    {
        switch($action)
        {
            case 'add':
                return 'add';
            case 'list':
                return 'glists';
            case 'qsearch':
                return 'qsearch';
            case 'update':
                return 'update';
            case 'delete':
                return 'delete';
            case 'cols':
                return 'get_columns';
            default:
                return 'glists';   
        }  
    }
    
    /**
     * Add a line in the table
     */
    public function add()  {
        $this->teditor->add($_REQUEST);
        echo json_encode(['errcode'=>0,'message'=>_('Success')]);
    }
    
    /**
     * List the data in the table
     */
    public function glists(){
        list($st,$end) = core::get_safe_input(INPUT_REQUEST, ['start','end'], ['int','int']);
        $st = ($st<=0)? 0:$st-1; //mysql start from 0
        $result = $this->teditor->lists($st,$end);
        echo json_encode($result,JSON_UNESCAPED_UNICODE);
    }
    
    /**
     * Quick search in the table
     */        
    public function qsearch() {
        list($cond,$order) = core::get_safe_input(INPUT_REQUEST, ['condition','order'], ['raw','int']);
        $od = $order ? 'ASC':'DESC';
        $result = $this->teditor->qsearch($cond, $od);
        echo json_encode($result,JSON_UNESCAPED_UNICODE);
    }
    
    /**
     * Update a line of data in the table
     */
    public function update(){
        try{
            $this->teditor->update($_REQUEST);
            echo json_encode(['errcode'=>0,'msg'=>_('Success')]);
        }catch(InvalidArgumentException $e)    {
            echo json_encode(['errcode'=>'4008','errmsg'=>$e->getMessage()], JSON_UNESCAPED_UNICODE);
        }
    }
    
    /**
     * Delete a line of data in the table
     */
    public function delete()    {
        try{
            list($id) = core::get_safe_input(INPUT_REQUEST, ['bid'], ['int']);
            $this->teditor->delete($id);
            echo json_encode(['errcode'=>0,'msg'=>_('Success')]);
        }catch(InvalidArgumentException $e){
            echo json_encode(['errcode'=>'4008','errmsg'=>$e->getMessage()], JSON_UNESCAPED_UNICODE);
        }    
    }
    
    /**
     * Reterive the columns in the table editor
     */
    public function get_columns()    {
        echo json_encode($this->teditor->getColumnFields());
    }
    
}
